import { createRouter, createWebHistory } from 'vue-router'
import IndexView from '../views/IndexView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'index',
      component: IndexView,
      redirect:"/homePage",
      children:[
        {
          path:"homePage",
          name:"homePage",
          component: () => import('../views/HomePage.vue')
        },
        {
          path:"/adminIndex",
          component:()=>import("../views/AdminIndex.vue"),
          // 默认显示
          redirect:"/adminIndex/modelOne",
          children:[
            {
              // 使用模态框显示添加/修改
              path:"/adminIndex/modelOne",
              component: () => import('../views/modelOne.vue')
            },
            {
               // 使用路由显示添加/修改
              path:"/adminIndex/administrators",
              name:"administrators",
              component: () => import('../views/AdministratorsPage.vue')
            },
            {
              path:"/adminIndex/addadmin",
              name:"addadmin",
              component: () => import('../views/AddAdmin.vue')
            },
          ]
        },
        {
          path:"userManagement",
          name:"userManagement",
          component:()=>import("../views/UserManagementPage.vue")
        },
        {
          path:"goodsManagement",
          name:"goodsManagement",
          component: () => import('../views/goodsManagementpage.vue')
        },
        {
          path:"goods",
          name:"goods",
          component: () => import('../views/练习子传父/Goods.vue')
        },
        {
          path:"/lianxiGoods",
          component: () => import('../views/练习路由跳转传参/lianxiGoods.vue'),
          redirect:"/lianxiGoods/lianxiOne",
          children:[
            {
              // 使用模态框显示添加/修改
              path:"/lianxiGoods/lianxiOne",
              component: () => import('../views/练习路由跳转传参/lianxiOne.vue')
            },
            {
              path:"/lianxiGoods/lianxiTwo",
              component: () => import('../views/练习路由跳转传参/lianxiTwo.vue')
            },
            ]
        },
       
      ]
    },
    {
      path: '/login',
      name: 'login',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/LoginView.vue')
    }
  ]
})
// 判断当前是否登录
router.beforeEach((to,from)=>{
  if(to.name!="login" && !sessionStorage.getItem("token")){
      return {
        name:"login"
      }
  }
})
export default router
